package com.huishen.edrivenew.ui;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.huishen.edrivenew.AppContext;
import com.huishen.edrivenew.Constants;
import com.huishen.edrivenew.R;
import com.huishen.edrivenew.base.BaseActivity;
import com.huishen.edrivenew.bean.LessInfoBean;
import com.huishen.edrivenew.net.JsonResponseHandler;
import com.huishen.edrivenew.net.NetApi;
import com.huishen.edrivenew.widget.MassageListener;
import com.huishen.edrivenew.widget.MessageDialog;

/**
 * 评价页面
 */
public class JudgeActivity extends BaseActivity {
    
    @InjectView(R.id.header_title)
    public TextView title;
    @InjectView(R.id.header_back)
    public ImageButton back;
    
    @InjectView(R.id.appoint_tv_time)
    public TextView date;
    @InjectView(R.id.appoint_tv_week)
    public TextView time;
    @InjectView(R.id.jugde_rb_fd)
    public RatingBar rat_td;
    @InjectView(R.id.jugde_rb_zl)
    public RatingBar rat_zl;
    
    @InjectView(R.id.judge_content)
    public EditText ed_comment;
    
    @InjectView(R.id.btn_start)
    public Button comBtn;
    
//    int id, int service, int teach, String body, int cohID
    private String sbody;
    private int id, service, teach, cohID;
    
    LessInfoBean bean;
    
    private MessageDialog mdialog;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_judge);
        ButterKnife.inject(this);
        
        setDate();
        
    }
    
    private void setDate() {
        title.setText("评价");
        
        bean = AppContext.getInstance().judgeBean;
        if(bean == null) {
            return;
        }
        id = bean.lessonInfoId;
        cohID = bean.cohId;
        
        date.setText(bean.lessonDate);
        time.setText(bean.timeFrame);

    }
    
    @OnClick(R.id.btn_start)
    public void login() {
        service = (int) rat_td.getRating();
        teach = (int) rat_zl.getRating();
        
        sbody = ed_comment.getText().toString();
        
        request(id, service, teach, sbody, cohID);
    }
    
    
    private void showMegDialog() {
        mdialog = new MessageDialog(this, "", "你已成功提交评价，我们会根据评价扣除学时，让你离考试更近一步", false,
            new MassageListener() {
                @Override
                public void setCommitClick() {
                       toMain();
                }
                @Override
                public void setCancelClick() {
                }
            });
        mdialog.setCancelable(false);
        mdialog.setCancelHide();
        mdialog.show();
    }
    
    private void request(int id, int service, int teach, String body, int cohID) {
        showDialog();
        NetApi.addStuEvalLessonInfo(this, new JsonResponseHandler() {
            @Override
            public void onSuccess(int code, JSONObject jsonObj, Object[] args) {
                dismissDialog();
                try {
                    if(code == Constants.N_SUCCESS) {
                        showMegDialog();
                    } else {
                        showToast("评价失败！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("评价失败！");
                }
            }
            @Override
            public void onFailure(int code, String errorMessage, Object[] args) {
                dismissDialog();
                showToast("评价失败！");
            }
            @Override
            public void onSessionFailure() {
                dismissDialog();
                super.onSessionFailure();
            }
        }, id, service, teach, body, cohID);
    }
    
    
    @OnClick(R.id.header_back)
    public void toMain() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        this.finish();
    }
    
}
